/*
 * @Author: huangyq
 * @Date: 2022-04-26 16:20:53
 * @LastEditTime: 2022-06-14 11:59:10
 * @LastEditors: huangyq
 * @Description: BaseApi
 * @FilePath: /vue3-boilerplate-pc-client/src/apis/BaseApi.ts
 */

import httpRequest, {
  IBaseRequest,
  AxiosRequestConfig,
  IBaseResponse,
  AxiosPromise
} from '@/plugins/httpHelper'

export default class BaseApi {
  static getBasePath(base: string) {
    return base.replace(/\/$/, '')
  }

  /**
   * @summary get
   */
  static baseGetRequest = (
    url: string,
    params: IBaseRequest,
    options?: Record<string, any>
  ): AxiosPromise<IBaseResponse> => {
    const payload: AxiosRequestConfig<IBaseRequest> = {
      method: 'get',
      url,
      params,
      ...options
    }
    return httpRequest(payload)
  }

  /**
   * @summary get
   */
  static baseDeleteRequest = (
    url: string,
    params: IBaseRequest,
    options?: Record<string, any>
  ): AxiosPromise<IBaseResponse> => {
    const payload: AxiosRequestConfig<IBaseRequest> = {
      method: 'get',
      url,
      params,
      ...options
    }
    return httpRequest(payload)
  }

  /**
   * @summary post
   */
  static basePostRequest = (
    url: string,
    params: IBaseRequest,
    options?: Record<string, any>
  ): AxiosPromise<IBaseResponse> => {
    const payload: AxiosRequestConfig<IBaseRequest> = {
      method: 'post',
      url,
      data: params,
      ...options
    }
    return httpRequest(payload)
  }

  /**
   * @summary put
   */
  static basePutRequest = (
    url: string,
    params: IBaseRequest,
    options?: Record<string, any>
  ): AxiosPromise<IBaseResponse> => {
    const payload: AxiosRequestConfig<IBaseRequest> = {
      method: 'put',
      url,
      data: params,
      ...options
    }
    return httpRequest(payload)
  }
}
